package com.flyme.module.exam.provider.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.exam.client.api.ExamRemoteService;
import com.flyme.module.exam.client.entity.Exam;
import com.flyme.module.exam.client.vo.ExamItem;
import com.flyme.module.exam.provider.service.ExamOptService;
import com.flyme.module.exam.provider.service.ExamService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author flyme
 */
@Slf4j
@Api(tags="题库管理")
@RestController
public class ExamController extends BaseController<ExamService,Exam> implements ExamRemoteService{
    @Autowired
    private ExamOptService examOptService;

    @Override
    public ResultBody list(PagerInfo pagerInfo, Exam exam) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("e.id,examTitle,examType.examTypeName,duanLevel,level,e.createTime,examSynopsis,frequency," +
            "(select count(ed.id) from exa_examrecorddetail ed where ed.examId = e.id and ed.isTrue = 1 and ed.examIdStamp = e.examIdStamp) examTrue," +
            "(select count(ed.id) from exa_examrecorddetail ed where ed.examId = e.id and ed.isTrue = 0 and ed.examIdStamp = e.examIdStamp) examfalse," +
            "(select count(ed.id) from exa_examrecorddetail ed where ed.examId = e.id and ed.examIdStamp = e.examIdStamp) examAll");
        cq.eq("examTypeId",exam.getExamTypeId());
        cq.like("examTitle",exam.getExamTitle());
        cq.eq("level",exam.getLevel());
        cq.eq("duanLevel",exam.getDuanLevel());
        cq.createAlias("examType","examType");
        PqGrid pqGrid = bizService.pageList(cq);
        List<EntityMap> list = (List<EntityMap>) pqGrid.getRows();
        for (EntityMap ex : list){
            Long examAll =  ex.get("examAll");
            if(examAll.equals(0L)){
                ex.put("accuracy", 0);
                continue;
            }
            Long et = ex.get("examTrue");
            BigDecimal all = new BigDecimal(examAll.toString());
            BigDecimal examTrue = new BigDecimal(et.toString());
            BigDecimal accuracy = examTrue.divide(all, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
            ex.put("accuracy", accuracy);
        }
        return ResultBody.success(pqGrid);
    }

    @Override
    public ResultBody add(Exam exam) {
        exam.setTargeType(1);
        exam.setFrequency(0);
        exam.setId(IdWorker.getId());
        exam.setExamIdStamp(exam.getId().toString() + System.currentTimeMillis());
        boolean result = bizService.save(exam);
        return ResultBody.success(result);
    }

    @Override
    public ResultBody get(@PathVariable Long id) {
        Exam course = bizService.getById(id);
        List<ExamItem> examItems=examOptService.selectList(id);
        course.setExamItems(JSONUtil.toJsonStr(examItems));
        return ResultBody.success().setData(course);
    }

    @Override
    public ResultBody update(Exam exam) {
        boolean result= bizService.update(exam);
        return ResultBody.success(result);
    }

    @Override
    public ResultBody delByIds(String[] ids) {
        return bizService.delByIds(ids);
    }

    @Override
    public ResultBody setExamStatus(Long id, Integer examStatus) {
        boolean result = bizService.set(id,"examStatus",examStatus);
        return ResultBody.ok("设置成功").setData(result);
    }
}
